﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Web;


namespace MVCPager
{
    public static class HtmlExtend
    {

        public static MvcHtmlString Pager(this UrlHelper urlHelper, string controllerName, string actionName, int pageSize, int pageIndex, int recordCount, string cssClass)
        {
            string pageStr = "";

            if (pageSize == 0)
            {
                return MvcHtmlString.Create(string.Empty);
            }

            int pageCount = (int)decimal.Ceiling((decimal)recordCount / (decimal)pageSize);

            string numStr = string.Empty;
            if (pageCount > 0)
            {
                // 遍历输出全部的页码
                for (int i = 1; i <= pageCount; i++)
                {
                    string numUrl = urlHelper.Action(actionName, controllerName, new { id = i });

                    // 当前页码加粗
                    if (i == pageIndex)
                    {
                        numStr += "<li><a href='" + numUrl + "'><strong>" + i + "</strong></a></li>";
                    }
                    else
                    {
                        numStr += "<li><a href='" + numUrl + "'>" + i + "</a><li> ";
                    }
                }
            }

            pageStr = "<ul class='" + cssClass + "'> " + numStr + "</ul> ";

            return MvcHtmlString.Create(pageStr);
        }
    }
}